﻿using System;
using System.Collections.Generic;
using DataBaseConnect.App;
using Entity;
using Microsoft.AspNetCore.Mvc;
using Utils;

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class DepartmentController : BaseController
    {
        #region 构造实例化

        private readonly IDepartmentAppService _departmentAppService;
        public DepartmentController(IDepartmentAppService departmentAppService)
        {
            _departmentAppService = departmentAppService;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddDepartment()
        {
            return View();
        }

        /// <summary>
        /// 编辑页
        /// </summary>
        /// <returns></returns>
        public IActionResult EditDepartment(int DepartmentId)
        {
            var user = _departmentAppService.GetDepartmentFirst(DepartmentId);
            return View(user);
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取所有部门&&查询
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDepartmentList(string DepartmentName)
        {
            ReceiveStatus<Department> receiveStatus = new ReceiveStatus<Department>();
            List<Department> list = new List<Department>();
            try
            {
                //得到所有部门
                list = _departmentAppService.GetDepartmentList(DepartmentName);
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="Input">部门信息实体</param>
        /// <returns></returns>
        public JsonResult AddDepartmentMsg(Department Input)
        {
            Message message = new Message();
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                receiveStatus= _departmentAppService.AddDepartmentMsg(Input);
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <param name="Input">部门信息实体</param>
        /// <returns></returns>
        public JsonResult EditDepartmentMsg(Department Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            Message message = new Message();
            receiveStatus = _departmentAppService.EditDepartmentMsg(Input);
            return Json(receiveStatus);
        }

        #endregion
    }
}